package com.wk.controller;

import com.wk.entity.CmfzUser;
import com.wk.service.CmfzGuruService;
import com.wk.service.CmfzUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private CmfzUserService cmfzUserService;

    @Autowired
    private CmfzGuruService cmfzGuruService;


    //利用多线程查询三个图标
    @RequestMapping("selectByThread")
    public Map selectByThread(){
        return  cmfzUserService.selectByThread();
    }

    @RequestMapping("regionCount")
    public List<Map> regionCount(){
        return cmfzUserService.regionCount();
    }

    @RequestMapping("RegisterIncrease")
    public List<Map> RegisterIncrease(){
        return cmfzUserService.RegisterIncrease();
    }


    @RequestMapping("selectByUserCount")
    public List<Map> selectByUserCount(){
        return cmfzUserService.selectBySexCount();
    }

    @RequestMapping("findAll")
    public Map findByCondition(Integer page,Integer limit,String value){
        return cmfzUserService.findByCondition(page,limit,value);
    }


    @RequestMapping("findGuru")
    public Map findGuru(){
        return cmfzGuruService.findGuru();
    }

    @RequestMapping("addUser")
    public Map addUser(CmfzUser cmfzUser){
        return cmfzUserService.addUser(cmfzUser);
    }

    @RequestMapping("findOne")
    public Map findOne(Integer id){
        return cmfzUserService.findOne(id);
    }

    @RequestMapping("update")
    @ResponseBody
    public Map update(HttpServletRequest request, MultipartFile picture2, CmfzUser cmfzUser){
        HashMap map = new HashMap();
        String originalFilename = picture2.getOriginalFilename();

        //重命名
        originalFilename = new Date().getTime()+""+originalFilename;

        //获取上传文件夹的绝对路径
        String realPath1 = request.getServletContext().getRealPath("/img");
        //上传
        try {
            picture2.transferTo(new File(realPath1+"/"+originalFilename));
        } catch (IOException e) {
            map.put("data","图片上传失败");
            e.printStackTrace();
            return map;
        }
        if(originalFilename==null||"".equals(originalFilename)){
            return cmfzUserService.update(cmfzUser);
        }
        cmfzUser.setUserImage(originalFilename);
        //修改
        return cmfzUserService.update(cmfzUser);
    }
}
